package com.example.basic;

import java.util.concurrent.locks.ReentrantLock;

public class LockTest implements Runnable{
    private Integer tickNum=100;
     ReentrantLock reentrantLock=new ReentrantLock();
    @Override
    public void run() {
        while (true){
            reentrantLock.lock();
            try {
                if(tickNum>0){
                    System.out.println(Thread.currentThread().getName()+"抢到了第"+(tickNum--)+"票");
                }else {
                    break;
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                reentrantLock.unlock();
            }
        }
    }
    public static void main(String[] args) {
        LockTest lockTest = new LockTest();
        new Thread(lockTest,"A").start();
        new Thread(lockTest,"B").start();
        new Thread(lockTest,"C").start();
        new Thread(lockTest,"D").start();
        new Thread(lockTest,"E").start();
    }
}
